package com.zyf.retrofitdemo.proxy;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;

/**
 * @author zyf
 * @date 2018/3/1
 * @time 15:07
 */

public class CglibFactory<T> {
    /**
     *
     * @param proxy
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getInstance(CglibDynamicProxy proxy, Class<T> clazz) {
        return getInstance(proxy, null, clazz);
    }

    /**
     *
     * @param proxy
     * @param callbackFilter
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getInstance(CglibDynamicProxy proxy, CallbackFilter callbackFilter, Class<T> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(proxy);
        if (callbackFilter != null) {
            enhancer.setCallbackFilter(callbackFilter);
        }
        return (T) enhancer.create();
    }

    public static <T> T getInstance(Callback[] callbacks, CallbackFilter callbackFilter, Class<T> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallbacks(callbacks);
        if (callbackFilter != null) {
            enhancer.setCallbackFilter(callbackFilter);
        }
        return (T) enhancer.create();
    }
}
